# coding:utf-8
import os
import sys
import binascii
from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin

class Token(models.Model):
    user = models.ForeignKey(User)
    token = models.CharField(max_length=40,primary_key=True,unique=True)
    created = models.DateTimeField(auto_now_add=True)

    def save(self,*args,**kwargs):
        if not  self.token:
            self.token = self.generate_token()
        return super(Token,self).save(*args,**kwargs)

    def generate_token(self):
        # return uuid.uuid1()

        return binascii.hexlify(os.urandom(20)).decode()
    def __unicode__(self):
        return self.token
    class Meta:
        verbose_name = u'Token'



admin.site.register(Token)